package com.ghh.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.ghh.constant.MessageConstant;
import com.ghh.dao.MemberDao;
import com.ghh.dao.OrderDao;
import com.ghh.dao.OrderSettingDao;
import com.ghh.entity.PageResult;
import com.ghh.entity.QueryPageBean;
import com.ghh.entity.Result;
import com.ghh.pojo.Member;
import com.ghh.pojo.Order;
import com.ghh.pojo.OrderSetting;
import com.ghh.service.OrderService;
import com.ghh.utils.DateUtils;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Date;
import java.util.Map;

@Service
public class OrderServiceImpl implements OrderService {
    @Autowired
    private OrderSettingDao orderSettingDao;
    @Autowired
    private MemberDao memberDao;
    @Autowired
    private OrderDao orderDao;

    @Override
    public PageResult pageQuery(QueryPageBean queryPageBean) {
        //定义需要的数据
        Integer currentPage = queryPageBean.getCurrentPage();
        Integer pageSize = queryPageBean.getPageSize();
        String queryString = queryPageBean.getQueryString();//ThreadLocal
        //分页插件，会在执行sql之前将分页关键字追加到SQL后面
        PageHelper.startPage(currentPage,pageSize);
        Page<Map<String,Object>> page = orderDao.findByCondition(queryString);
        return new PageResult(page.getTotal(),page.getResult());
    }

    @Override
    public Result order(Map map) throws Exception {
        //1、检查用户所选择的预约日期是否已经提前进行了预约设置，如果没有设置则无法进行预约
        //获取预约日期
        String orderDate = (String) map.get("orderDate");
        //查询有无该日期的预约设置
        OrderSetting orderSetting = orderSettingDao.findByOrderDate(DateUtils.parseString2Date(orderDate));
        if (orderSetting == null){
            //该日期没有预约设置，无法完成体检预约
            return new Result(false, MessageConstant.SELECTED_DATE_CANNOT_ORDER);
        }

        //2、检查用户所选择的预约日期是否已经约满，如果已经约满则无法预约
        //该日期可预约人数
        int number = orderSetting.getNumber();
        //该日期已预约人数
        int reservations = orderSetting.getReservations();
        //对比人数
        if (reservations >= number){
            //预约已满
            return new Result(false, MessageConstant.ORDER_FULL);
        }

        //3、检查当前用户是否为会员，如果是会员则检查是否重复预约，如果不是会员则自动完成注册并进行预约
        //获取预约用户的手机号
        String telephone = (String) map.get("telephone");
        //查询该手机号是否已注册
        Member member = memberDao.findByTelephone(telephone);
        if (member!=null){//说明注册过
        //4、检查用户是否重复预约（同一个用户在同一天预约了同一个套餐），如果是重复预约则无法完成再次预约
        // 获取该用户的会员id
            Integer memberId = member.getId();
            //获取预约日期
            Date order_Date = DateUtils.parseString2Date(orderDate);
            //获取预约的套餐ID
            String setmealId = (String) map.get("setmealId");
            //封装
            Order order = new Order(memberId, order_Date, Integer.parseInt(setmealId));
            //在数据可进行条件查询
            Integer count = orderDao.findCountByCondition(order);
            if (count!=0){
                //说明在重复预约
                return new Result(false,MessageConstant.HAS_ORDERED);
            }
        }else {//说明没注册过
            //自动进行会员注册
            member = new Member();
            member.setName((String) map.get("name"));
            member.setPhoneNumber(telephone);
            member.setIdCard((String) map.get("idCard"));
            member.setSex((String) map.get("sex"));
            member.setRegTime(new Date());
            memberDao.add(member);//自动完成会员注册

        }

        //5、预约成功，更新当日的已预约人数
        //添加预约信息到数据库
        Order order = new Order();
        order.setMemberId(member.getId());//设置会员ID
        order.setOrderDate(DateUtils.parseString2Date(orderDate));//预约日期
        order.setOrderType((String) map.get("orderType"));//预约类型
        order.setOrderStatus(Order.ORDERSTATUS_NO);//到诊状态
        order.setSetmealId(Integer.parseInt((String) map.get("setmealId")));//套餐ID
        order.setOrderCompany_id((Integer) map.get("orderCompany_id"));
        orderDao.add(order);

        //将预约设置的已预约人数+1
        orderSetting.setReservations(orderSetting.getReservations() + 1);
        orderSettingDao.editReservationsByOrderDate(orderSetting);

        return new Result(true,MessageConstant.ORDER_SUCCESS,order.getId());
    }

    @Override
    public Map findById(Integer id) throws Exception {
        Map map = orderDao.findById(id);
        if (map!=null){
            //处理日期格式
            Date orderDate = (Date) map.get("orderDate");
            map.put("orderDate",DateUtils.parseDate2String(orderDate));
        }
        return map;
    }
}
